package com.duoketech.service.deliveryconfig;
import com.duoketech.service.AbstractService;
import com.duoketech.model.deliveryconfig.DeliveryConfig;
import com.duoketech.exception.ValidateException;
import com.duoketech.utils.PageUtil;
import com.duoketech.utils.search.EqualCondition;
import com.duoketech.utils.search.QueryConstructor;
import com.duoketech.utils.validator.Validator;
import com.duoketech.utils.validator.rules.string.StringLengthBetween;
import com.duoketech.utils.validator.rules.string.StringMustBeRequired;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;
import java.util.Map;
public class DeliveryConfigService extends AbstractService{
    public static DeliveryConfigService ME = new DeliveryConfigService();

    @Override
    public void validatorUpdate(Model<?> model, Kv kv) throws ValidateException {
        validatorAdd(model,kv);
    }

    @Override
    public void validatorAdd(Model<?> model, Kv kv) throws ValidateException {

        Validator validator = new Validator();
        validator.addRule(new StringMustBeRequired(model.getStr(DeliveryConfig.NAME),"快递名称项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(DeliveryConfig.NAME),"快递名称项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(DeliveryConfig.FIRST_WEIGHT),"首重项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(DeliveryConfig.FIRST_WEIGHT),"首重项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(DeliveryConfig.ADDITIONAL_WEIGHT),"续重项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(DeliveryConfig.ADDITIONAL_WEIGHT),"续重项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(DeliveryConfig.AREA),"运送区域项为必填信息"));
        validator.addRule(new StringMustBeRequired(model.getStr(DeliveryConfig.FIRST_WEIGHT_PRICE),"首重项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(DeliveryConfig.FIRST_WEIGHT_PRICE),"首重项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(DeliveryConfig.ADDITIONAL_WEIGHT_PRICE),"续重项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(DeliveryConfig.ADDITIONAL_WEIGHT_PRICE),"续重项字符必须为1-50个字符",1,50));
        validator.excute();

        String[] arr = model.getStr(DeliveryConfig.NAME).split(",");
        model.set(DeliveryConfig.NAME,arr[0]);
        model.set(DeliveryConfig.DELIVERY_CODE,arr[1]);
        model.set(DeliveryConfig.FIRST_WEIGHT,PageUtil.kg2G(1));
        model.set(DeliveryConfig.ADDITIONAL_WEIGHT,PageUtil.kg2G(1));
    }

    @Override
    public void beforAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public String tableName() {
        return DeliveryConfig.dao.tableName();
    }

    @Override
    public void builderSQLExpected(Model<?> model, QueryConstructor queryConstruct, Kv kv) {
    	int deliveryCode = model.getInt(DeliveryConfig.DELIVERY_CODE);
    	if(deliveryCode>0){
    		queryConstruct.and(new EqualCondition(DeliveryConfig.DELIVERY_CODE,deliveryCode));
    	}
    }


    public List<Record> queryRegionOfProvince(){
        String sql="select * from t_region where parent_id=0";
        return Db.find(sql);
    }


    public DeliveryConfig queryDeliveryConfigBy(String province,int deliveryType){
        String sql="select * from "+DeliveryConfig.dao.tableName()+" where area like ? and delivery_code=?";
        DeliveryConfig deliveryConfig =DeliveryConfig.dao.findFirst(sql,"%"+province+"%",deliveryType);
        return deliveryConfig;
    }

    public double caculateDeliveryFee(int totalWeight_G,String province,int deliveryType){
        double fee = 0;
        DeliveryConfig deliveryConfig = queryDeliveryConfigBy(province,deliveryType);
        if(deliveryConfig!=null){
            //检查当前订单总重量是否为0；
            if(totalWeight_G==0){
                return fee;
            }

            //计算首重是否超过1KG
            int left = totalWeight_G-1000;
            if(left<=0){//假如货物不足一公斤
                fee = deliveryConfig.getInt(DeliveryConfig.FIRST_WEIGHT_PRICE);
                return fee;
            }else{//假如货物超过一公斤
                double fisrtWeight = deliveryConfig.getInt(DeliveryConfig.FIRST_WEIGHT_PRICE);
                double additionalWeight = deliveryConfig.getInt(DeliveryConfig.ADDITIONAL_WEIGHT_PRICE);
                double leftKG = PageUtil.g2Kg(left);
                fee =(fisrtWeight)+(additionalWeight*leftKG);
            }
            return fee;
        }
        return fee;
    }
}
